﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExtAspNet;
using BLL;

namespace WebUI.LabManage
{
    public partial class Consumable : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bn_add.OnClientClick = window1.GetShowReference("ConsumableEdit.aspx?action=new", "新增易耗品");
                BindForm();
                BindGrid();
            }
            if (Request[Common.LinkService.RefreshTag] == Common.LinkService.RefreshString)
            {
                BindGrid();
            }
        }

        private void BindGrid()
        {
            Grid1.DataSource = ConsumableManage.GetList().Tables[0];
            Grid1.DataBind();
        }
        private void InitForm()
        {
            dl_location.SelectedValue = "-1";
            tb_name.Text = "";
        }
        private void BindForm()
        {
            dl_location.DataSource = BLL.Parameter.GetParamList("9");
            dl_location.DataTextField = "value";
            dl_location.DataValueField = "id";
            dl_location.DataBind();
            ExtAspNet.ListItem item = new ExtAspNet.ListItem("全部", "-1");
            dl_location.Items.Insert(0, item);
        }

        protected void Grid1_PageIndexChange(object sender, ExtAspNet.GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
        }
        protected void Grid1_RowClick(object sender, ExtAspNet.GridRowClickEventArgs e)
        {
            PageContext.RegisterStartupScript(view_window.GetShowReference(string.Format("ConsumableEdit.aspx?action=view&id={0}", Grid1.DataKeys[e.RowIndex][0].ToString()), "查看 - 易耗品"));
        }
        protected void Grid1_RowCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == "Del")
            {
                int id = int.Parse(Grid1.DataKeys[e.RowIndex][0].ToString());
                ConsumableManage.DeleteByID(id);
                BindGrid();
            }
        }
        protected void bnReset_Click(object sender, EventArgs e)
        {
            System.Threading.Thread.Sleep(800);
            BindForm();
            InitForm();
            BindGrid();
        }
        protected void bnSearch_Click(object sender, EventArgs e)
        {
            System.Threading.Thread.Sleep(800);
            StringBuilder strSql = new StringBuilder();
            if (tb_name.Text != "")
            {
                strSql.AppendFormat(" name LIKE '{0}'",tb_name.Text);
            }
            if (dl_location.SelectedValue != "-1")
            {
                if (tb_name.Text != "")
                {
                    strSql.Append(" AND");
                }
                strSql.AppendFormat(" location = {0}",dl_location.SelectedValue);
            }
            Grid1.DataSource = ConsumableManage.GetList(strSql.ToString());
            Grid1.DataBind();
        }

    }
}